﻿using HslCommunication.Profinet.Melsec;
using Newtonsoft.Json.Linq;
using Sylan.Common.Util;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Eps.Ai.Common
{
    public class UpperComputerUtil
    {
        private MelsecFxSerial melsecSerial = null;

        /// <summary>
        /// 打开串口
        /// </summary>
        /// <param name="PortName">com地址</param>
        /// <param name="BaudRate">波特率</param>
        public void OpenSerialPort(string PortName,int BaudRate,int DataBits, StopBits stopBits, Parity Parity)
        {
            Logger.Write(Level.INFO, "打开串口");
            try
            {
                melsecSerial?.Close();
                melsecSerial = new MelsecFxSerial();
                melsecSerial.SerialPortInni(sp =>
                {
                    sp.PortName = PortName;
                    sp.BaudRate = BaudRate;
                    sp.DataBits = DataBits;
                    sp.StopBits = stopBits;
                    sp.Parity = Parity;
                });
                melsecSerial.Open();
            }
            catch (Exception ex)
            {
                Logger.Write(Level.ERROR, "打开串口异常",ex);
                throw ex;
            }
            
        }

        /// <summary>
        /// 关闭串口
        /// </summary>
        public void CloseSerialPort()
        {
            // 断开连接
            melsecSerial.Close();
        }

        /// <summary>
        /// 发送X命令消息
        /// </summary>
        /// <param name="types">True</param>
        /// <param name="msg">X0、X3</param>
        public JObject SendMsg(string types,string msg)
        {
            return DemoUtils.WriteResultRender(() => melsecSerial.Write(msg, bool.Parse(types)), msg);
        }

        /// <summary>
        /// 发送命令消息
        /// </summary>
        /// <param name="types">True</param>
        /// <param name="msg">X0、X3</param>
        public JObject SendMsgBoolean(string action, bool value)
        {
            return DemoUtils.WriteResultRender(() => melsecSerial.Write(action, value), action);
        }

        /// <summary>
        /// 发送速度消息
        /// </summary>
        /// <param name="action">D100</param>
        /// <param name="value">100</param>
        public JObject SendMsgInt(string action, int value)
        {
            return DemoUtils.WriteResultRender(() => melsecSerial.Write(action, value), action);
        }

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="action">D100</param>
        /// <param name="value">100</param>
        public JObject SendMsgShort(string action, int value)
        {
            return DemoUtils.WriteResultRender(() => melsecSerial.Write(action, value), action);
        }

        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public JObject ReadResults(string msg)
        {
            if (melsecSerial == null)
                return null;
            return DemoUtils.ReadResultRender(melsecSerial.ReadInt32(msg), msg, null);

            
        }

        /// <summary>
        /// 读取数据.16位寄存器
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public JObject ReadResultsShort(string msg)
        {
            if (melsecSerial == null)
                return null;
            return DemoUtils.ReadResultRender(melsecSerial.ReadInt16(msg), msg, null);
        }

        /// <summary>
        /// 读取字符数据
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public JObject ReadResultsString(string msg,ushort length)
        {
            if (melsecSerial == null)
                return null;
            return DemoUtils.ReadResultRender(melsecSerial.ReadString(msg, length), msg, null);
        }

        /// <summary>
        /// 读取bool数据
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public JObject ReadBoolResults(string msg)
        {
            if (melsecSerial == null)
                return null;
            return   DemoUtils.ReadResultRender(melsecSerial.ReadBool(msg), msg, null);

        }



    }
}
